package com.xgkj.diyiketang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.IsHaveOpenIDTask;
import com.xgkj.diyiketang.task.TiXianTask;
import com.xgkj.diyiketang.wxapi.WXEntryActivity;
import com.xgkj.diyiketang.yizhibo.Constants;

public class TixianXuefenActivity extends BaseActivity implements View.OnClickListener {
    private TextView back,weixin_nick;
    private EditText tixian_money;
    private Button tixian_btn;
    private WeiXinReceiver wxReceiver;
    IWXAPI wxApi = null;
    private String openidString;
    private String thirdIconUrl;
    private String nicknameString;
    private int isHaveOPenTaskID;
    private int thirdType;
    private int tixiantaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tixian_xuefen);
        wxApi = WXAPIFactory.createWXAPI(getApplicationContext(), null);
        wxApi.registerApp(ProtocolConst.APP_ID);
        initUI();
    }

    private void initUI() {
        back = (TextView) findViewById(R.id.zhuanyun_back);
        back.setOnClickListener(this);
        weixin_nick = (TextView) findViewById(R.id.weixin_nick);
        weixin_nick.setOnClickListener(this);
        tixian_money = (EditText) findViewById(R.id.tixian_money);
        tixian_btn = (Button) findViewById(R.id.live_prepare_btn);
        tixian_btn.setOnClickListener(this);
        String have_bind = EatShowApp.getACache().getAsString(Constants.HAVE_BIND_PAY);
        String nick_name = EatShowApp.getACache().getAsString(Constants.WEIXIN_NICKNAME);
        if (have_bind.equals("0")){
            weixin_nick.setText("请绑定微信");
            tixian_money.setEnabled(false);
        }else {
            weixin_nick.setText(nick_name);
            tixian_money.setEnabled(true);
            weixin_nick.setEnabled(false);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zhuanyun_back:
                finish();
                break;
            case R.id.weixin_nick:
                thirdType = 0;
                sendWxAuthRequest();
                break;
            case R.id.live_prepare_btn:
                String money = tixian_money.getText().toString();
                int type = 1;
                doTiXian(money,type);
                break;
        }
    }
    private void doTiXian(String jineString,int type){
        TiXianTask task = new TiXianTask(jineString,null,"",type);
        task.addListener(new LoginTaskEventListener(mContext));
        tixiantaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    /**
     * 微信登录获取openId
     */
    private void sendWxAuthRequest() {
        if (wxApi.isWXAppInstalled()) {
            if (wxReceiver == null) {
                wxReceiver = new WeiXinReceiver();
                IntentFilter filter = new IntentFilter();
                filter.addAction(WXEntryActivity.WEIXIN_LOGIN_SUCCESS_ACTION);
                registerReceiver(wxReceiver, filter);

            }
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk_demo";
            //拉起微信授权，授权结果在WXEntryActivity中接收处理
            wxApi.sendReq(req);
        } else {
            Toast.makeText(this, "微信未安装", Toast.LENGTH_SHORT).show();
        }
    }
    class WeiXinReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(WXEntryActivity.WEIXIN_LOGIN_SUCCESS_ACTION)) {
                if (wxReceiver != null) {
                    unregisterReceiver(wxReceiver);
                    wxReceiver = null;
                }
                openidString = intent.getStringExtra(ProtocolConst.WEIXIN_OPENID);
                thirdIconUrl = intent.getStringExtra("LOGOURL");
                nicknameString = intent.getStringExtra("NICK");
                isHaveOpenId();
            }
        }
    }
    public void isHaveOpenId() {
        IsHaveOpenIDTask isTask = new IsHaveOpenIDTask(openidString);
        isTask.addListener(new LoginTaskEventListener(mContext));
        isHaveOPenTaskID = getEatShowApp().getTaskManager().addTask(isTask);
    }
    class LoginTaskEventListener extends ShowToastTaskEventListener {
        public LoginTaskEventListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);

            if (taskId == isHaveOPenTaskID) {
                if (data != null) {

                }
            }else if (taskId == tixiantaskId){

            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            getEatShowApp().setmUserInfo(null);
        }
    }
}
